package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.AllStackViewHolder;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.ListWeekBookResopnse;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class BillboardActivity extends BaseActivity {

    private RadioGroup tabLayout;
    private RecyclerView rv_find;
    private final String[] TAB_TITLES = new String[]{"月榜", "季榜", "半年榜", "年榜"};
    private BillboardAdapter adapter;
    private int type = 1;

    private String mainTab;

    @Override
    public int getContentViewId() {
        return R.layout.activity_billboard;
    }

    @Override
    public void findView() {
        tabLayout = findViewById(R.id.tab_find_layout);
        rv_find = findViewById(R.id.rv_find);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onBackPressed(View v) {
        super.onBackPressed(v);
        MobclickAgent.onEvent(this, UMConstant.SUGGEST_FENLEI3_MORE_BACK_CLICK);
    }

    @Override
    public void afterCreate() {
        mainTab = getIntent().getStringExtra("mainTab");
        type = getIntent().getIntExtra("mTab", 0) + 1;
        rv_find.setNestedScrollingEnabled(false);
        rv_find.setFocusable(false);
        rv_find.setLayoutManager(new LinearLayoutManager(BillboardActivity.this));
        rv_find.addItemDecoration(new LinearLayouDecoration(BillboardActivity.this,
                LinearLayouDecoration.HORIZONTAL, R.dimen.border8, R.color.transparent));
        rv_find.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new BillboardAdapter(this);
        rv_find.setAdapter(adapter);
        setTabs(tabLayout, getLayoutInflater(), TAB_TITLES);
        getListDateRankBook();
    }

    private void setTabs(RadioGroup tabLayout, LayoutInflater inflater, String[] tabTitlees) {
        tabLayout.removeAllViews();
        for (int i = 1; i <= tabTitlees.length; i++) {
            RadioButton view = (RadioButton) inflater.inflate(R.layout.find_item_content, null);
            view.setId(i);
            ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, FFUtils.getPx(30));
            layoutParams.setMargins(15, 0, 0, 0);
            view.setLayoutParams(layoutParams);
            if (i == type) {
                view.setChecked(true);
            } else {
                view.setChecked(false);
            }
            view.setText(tabTitlees[i - 1]);
            tabLayout.addView(view);
            int finalI = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    type = finalI;
                    getListDateRankBook();
                    if (finalI == 1) {
                        MobclickAgent.onEvent(BillboardActivity.this, UMConstant.SUGGEST_FENLEI3_MORE_MONTH_CLICK);
                    } else if (finalI == 2) {
                        MobclickAgent.onEvent(BillboardActivity.this, UMConstant.SUGGEST_FENLEI3_MORE_QUARTER_CLICK);
                    } else if (finalI == 3) {
                        MobclickAgent.onEvent(BillboardActivity.this, UMConstant.SUGGEST_FENLEI3_MORE_HALF_YEAR_CLICK);
                    } else if (finalI == 4) {
                        MobclickAgent.onEvent(BillboardActivity.this, UMConstant.SUGGEST_FENLEI3_MORE_YEAR_CLICK);
                    }
                }
            });

        }


    }

    public void getListDateRankBook() {
        post(Url.listDateRankBook, "", ListWeekBookResopnse.class, new FFNetWorkCallBack<ListWeekBookResopnse>() {
            @Override
            public void onSuccess(ListWeekBookResopnse response) {
                if (response.getCode() == 200) {
                    adapter.setData(response.getData());
                }

            }

            @Override
            public boolean onFail(ListWeekBookResopnse response) {
                return false;
            }
        }, "{\"offset\":0,\"limit\":10,\"tab\":\"" + mainTab + "\",\"rankType\":\"" + type + "\"}");
    }

    public class BillboardAdapter extends BaseRecyclerAdapter<AllStackViewHolder> {
        private Context context;


        private List<ListWeekBookResopnse.DataBean> mData = new ArrayList<>();


        public BillboardAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<ListWeekBookResopnse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<ListWeekBookResopnse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public AllStackViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.allstack_item_list, parent, false);
            AllStackViewHolder viewHolder_text = new AllStackViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public AllStackViewHolder getViewHolder(View view) {
            return new AllStackViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        @Override
        public void onBindViewHolder(final AllStackViewHolder holder, @SuppressLint("RecyclerView") final int position, boolean isItem) {
            FFImageLoader.loadImage(context, holder.img_allstack_item, mData.get(position).getCoverImgUrl() + "", mData.get(position).getBookId(), true);
            holder.tv_bookname_allstack.setText(mData.get(position).getName());
            holder.tv_book_author.setText(mData.get(position).getAuthor());
            holder.tv_book_info.setText(StUtils.reBlank(mData.get(position).getBookIntro()));
            holder.tv_allstack_tab.setText(mData.get(position).getChannlname());
            holder.cl_allstack_list.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(BillboardActivity.this, BookDetailsActivity.class);
                    intent.putExtra("bookId", mData.get(position).getBookId());
                    startActivity(intent);

                }
            });


        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }

}
